<?php
namespace UserApi\Controller;
use Zend\Mvc\Controller\AbstractRestfulController;
use Zend\View\Model\JsonModel;
class OrderApiController extends AbstractRestfulController
{
    public function getList()
    {
		header('Access-Control-Allow-Origin: *');
    }
    public function get($id)
    {	
		header('Access-Control-Allow-Origin: *');
		$userMedicineOrderTable=$this->getServiceLocator()->get('Models\Model\UserMedicineOrderFactory');
		$explodeId=explode('-',$id);
		if($explodeId[0]=='user'){
			$user_id=$explodeId[1];
			$totalOrders = $userMedicineOrderTable->totalOrders($user_id)->toArray();
			if(count($totalOrders)!=0){
				return new JsonModel(array(
					'value'			=>	1,
					'result'		=> $totalOrders,
				));
			}else{
				return new JsonModel(array(
					'value'			=>	0,
				));
			}
		}else if($explodeId[0]=='order'){
			$order_id=$explodeId[1];
			$user_id=$explodeId[2];
			$checkOrder = $userMedicineOrderTable->checkOrder($order_id,$user_id);
			if($checkOrder!=0){
				$orderStatus = $userMedicineOrderTable->orderStatus($order_id)->current()->status;
				if($orderStatus==0){
					return new JsonModel(array(
						'value'		    => 	0,
					));
				}else if($orderStatus==1){
					return new JsonModel(array(
						'value'		    => 1,
					));
				}else if($orderStatus==2){
					return new JsonModel(array(
						'value'		    => 2,
					));
				}else{
					return new JsonModel(array(
						'value'		    => 3,
					));
				}
			}else{
				return new JsonModel(array(
					'value'		    => 4,
				));
			}
		}else{
		   $order_id=$explodeId[1];
		   $order = $userMedicineOrderTable->getuserorderReorder($order_id)->toarray();
		   // echo "<pre>";print_r($order);exit;
		   
		   if( isset($order) && isset($order[0]) && isset($order[0]['prescription_details']) )
		   {
				$prescription_details=trim($order[0]['prescription_details']);
				$prescriptionsArr = explode( "**",$prescription_details );
				
				$tabletName = "";
				$tabletQuantity = 0;
				if( isset($prescriptionsArr) )
				{
					if( count($prescriptionsArr) > 0 )
					{
						$order[0]['dosageDetails']=array();
					}
					if( count($prescriptionsArr) == 1 )
					{
						$order[0]["tabletNamee"]="";
						$order[0]["quantityy"]="";
					}
					for( $currDosageId = 0; $currDosageId < count($prescriptionsArr); $currDosageId++ )
					{
						$dosageDetailsArr = explode( "$$",$prescriptionsArr[$currDosageId] );
						if( isset($dosageDetailsArr) && isset($dosageDetailsArr[0]) && isset($dosageDetailsArr[1]) )
						{
							$tabletName = $dosageDetailsArr[0];
							$tabletQuantity = $dosageDetailsArr[1];
						}
						if( $currDosageId == 0 )
						{
							$order[0]['tabletName'] = $tabletName;
							if( $tabletQuantity != "" )
							{
								$order[0]['quantity'] = intval($tabletQuantity);
							}
							else
							{
								$order[0]['quantity'] = intval("0");
							}
						}
						else
						{
							$order[0]['tabletNamee'][$currDosageId] = $tabletName;
							if( $tabletQuantity != "" )
							{
								$order[0]['quantityy'][$currDosageId] = intval($tabletQuantity);
							}
							else
							{
								$order[0]['quantityy'][$currDosageId] = intval("0");
							}
						}
					}
				}
		   }
		   // echo "<pre>";print_r($order);exit;
		   
		   return new JsonModel(array(
					'value'		    => 1,
					'data' =>$order,
				));
		}
    }
    public function create($order)
    {	
		header('Access-Control-Allow-Origin: *');
		$images="";
		if( isset($_POST) && !empty($_POST) )
		{
			if( isset($_FILES) && !empty($_FILES) )
			{
				move_uploaded_file( $_FILES[ 'file' ][ 'tmp_name' ],'./public/uploads/prescription/'.$_FILES[ 'file' ][ 'name' ] );
				$userMedicineOrderTable=$this->getServiceLocator()->get('Models\Model\UserMedicineOrderFactory');
				$trackingId = $_POST["trackingId"];
				$imgNumber = $_POST["imgNumber"];
				$updateStatus = $userMedicineOrderTable->updatePresImages( $trackingId,$_FILES[ 'file' ][ 'name' ],$imgNumber );
				return new JsonModel(array(
					'updateStatus'		=> $updateStatus
				));
			}
			exit;
		}
		else
		{
			$prescription_details="";
			$details="";
			$userMedicineOrderTable=$this->getServiceLocator()->get('Models\Model\UserMedicineOrderFactory');
			$userDetailsTable=$this->getServiceLocator()->get('Models\Model\UserDetailsFactory');
			$notificationTable=$this->getServiceLocator()->get('Models\Model\NotificationsFactory');
			if($order['oldorderId']==0){
				if($order['tabletName']!=""){
					$prescription_details.=$order['tabletName'].'$$'.$order['quantity'].'**';
					if(isset($order['tabletNamee'])){
						foreach($order['tabletNamee'] as $key=>$tabletName){
							$prescription_details.=$tabletName.'$$'.$order['quantityy'][$key].'**';
						}
					}
					$details=trim($prescription_details,'**');
				}

				$imagess="";
				if( isset($order['prescription_upload_path']) && trim($order['prescription_upload_path']) != "0" )
				{
					$imagess=$order['prescription_upload_path'];
				}
				else
				{
					$imagess=trim($imagess,'**');
				}

				$sendOrder = $userMedicineOrderTable->saveOrder($order,$details,$imagess);
				
				$sendNotification = $notificationTable->sendAdminNotification($sendOrder,$order,'2','0','1');
				$usersendOrderDetails = $userDetailsTable->updateorderDetailes($order,$order['user_id']);			
			}else{
				$getOrderDetails = $userMedicineOrderTable->getOrderDetails($order['oldorderId']);
				$orderNew=$getOrderDetails->toarray();
				$sendOrder = $userMedicineOrderTable->saveReOrder($orderNew[0]);
				$sendOrderr = $userMedicineOrderTable->changeAutoRenewalReorder($order['oldorderId']);
			}
			if($sendOrder!=0){
				return new JsonModel(array(
					'TrackingId'	=>	$sendOrder,
					'status'		=> 	'success',
					'value'			=>	1,
				));
			}else{
				return new JsonModel(array(
					'status'		=> 'fail',
					'value'			=>	0,
				));
			}
		}
    }
    public function update($order_id,$order)
    {
		header('Access-Control-Allow-Origin: *');
		$userMedicineOrderTable=$this->getServiceLocator()->get('Models\Model\UserMedicineOrderFactory');
		$orderDeliveryTable=$this->getServiceLocator()->get('Models\Model\OrderDeliveryDetailsFactory');
		$notificationTable=$this->getServiceLocator()->get('Models\Model\NotificationsFactory');
		$cancelledOrder = $userMedicineOrderTable->cancelledOrder($order_id);
		$getuserId= $userMedicineOrderTable->getUserId($order_id)->toArray();
		if(count($getuserId)!=0){
			$sendCancelledNotification=$notificationTable->sendcancelledComplaint($order,'1','0','0','0','2',$getuserId[0]['user_id']);
		}
		$cancelledOrderr = $orderDeliveryTable->deleteOrders($order_id);
		if(count($cancelledOrder)!=0){
		   return new JsonModel(array(
					'status'			=>	'success',
					'value'             =>  1,
				));	  
		}else{
		  return new JsonModel(array(
					'status'			=>	'fail',
					'value'             =>  0,
				));		  
		}
    }
    public function delete($id)
    {
       
    }
}